package middleware

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"

	"2106a-zg5/work/day15/user_api/utils"
)

// TokenAuth 中间件
func TokenAuth() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.Request.Header.Get("Authorization")
		fmt.Println(token)
		uid, _, err := utils.ParesToken(token)
		if err != nil {
			c.JSON(http.StatusBadRequest, &gin.H{
				"msg":  err.Error(),
				"code": -1,
				"data": nil,
			})
			c.Abort()
			return
		}
		c.Set("uid", uid)
		c.Next()
	}
}
